<?php 
namespace app\agent\controller;

use app\common\model\user\Users;
use app\common\model\trade\TradeLeveraged;
use think\Db;
use app\common\model\cash\CashRecharge;
use app\common\model\cash\CashList;

class Usercoin extends Base
{
	/**
	 * 交易记录
	 * @return \think\response\View
	 */
	public function trade()
	{
		$data = $this->request->param('');
		//初始化查询条件
		$where = [];
		$mobile = !empty($data['mobile']) ? $data['mobile'] : '';
		$trade_style = !empty($data['trade_style']) ? $data['trade_style'] : '';
		$start_time = !empty($data['start_time']) ? $data['start_time'] : '';
		$end_time = !empty($data['end_time']) ? $data['end_time'] : '';
		if($mobile){
			$user_id = Users::where(['mobile' => $mobile])->value('id');
			$where['user_id'] = $user_id;
		}else{
			//获取所有会员
			$user_ids = Users::where(['act_id' => $this->agent_id])->column('id');
			$where['user_id'] = ['in',$user_ids];
		}
		if($trade_style) $where['trade_style'] = $trade_style;
		if($start_time && $end_time) {
			if(strtotime($end_time) > strtotime($start_time)) {
				$where['create_time'] = ['between',[strtotime($start_time),strtotime($end_time)]];
			}
		}
		$list = TradeLeveraged::where($where)->order('id desc')->paginate(15,false,[
				'query' => $data
		]);
		$page = $list->render();
		$list = $list->toArray();
		foreach ($list['data'] as &$rows) {
			$rows['user_info'] = Users::get($rows['user_id']);
		}
		//流水汇总
		$finance = $this->getTradeList($data);
		$this->assign('list',$list);
		$this->assign('page',$page);
		$this->assign('mobile',$mobile);
		$this->assign('trade_style',$trade_style);
		$this->assign('start_time',$start_time);
		$this->assign('end_time',$end_time);
		$this->assign('finance',$finance);
		return view();
	}
	
	/**
	 * 历史汇总
	 * @param unknown $data
	 * @return number[][]
	 */
	private function getTradeList($data)
	{
		$start_time = strtotime(date('Y-m-d').'00:00:00');
		$end_time = strtotime(date('Y-m-d').'23:59:59');
		//初始化查询条件
		$where_buy = ['trade_style' => 1,'close' => ['exp',Db::raw('> buy_price')],'state' => ['neq',2]];
		$where_sale = ['trade_style' => 2,'close' => ['exp',Db::raw('< buy_price')],'state' => ['neq',2]];
		$where_buy_down = ['trade_style' => 1,'close' => ['exp',Db::raw('< buy_price')],'state' => ['neq',2]];
		$where_sale_down = ['trade_style' => 2,'close' => ['exp',Db::raw('> buy_price')],'state' => ['neq',2]];
		$where_day_buy = ['create_time' => ['between',[$start_time,$end_time]],'trade_style' => 1,'close' => ['exp',Db::raw('> buy_price')],'state' => ['neq',2]];
		$where_day_buy_down = ['create_time' => ['between',[$start_time,$end_time]],'trade_style' => 1,'close' => ['exp',Db::raw('< buy_price')],'state' => ['neq',2]];
		$where_day_sale = ['create_time' => ['between',[$start_time,$end_time]],'trade_style' => 2,'close' => ['exp',Db::raw('< buy_price')],'state' => ['neq',2]];
		$where_day_sale_down = ['create_time' => ['between',[$start_time,$end_time]],'trade_style' => 2,'close' => ['exp',Db::raw('> buy_price')],'state' => ['neq',2]];
		$where_fee = ['state' => ['neq',2]];
		$where_fee_day =['create_time' => ['between',[$start_time,$end_time]],'state' => ['neq',2]];
		if(!empty($data['mobile'])) {
			$user_id = Users::where(['user_name' => $data['mobile']])->value('id');
			$where_buy['user_id'] = $user_id;
			$where_sale['user_id'] = $user_id;
			$where_buy_down['user_id'] = $user_id;
			$where_sale_down['user_id'] = $user_id;
			$where_day_buy['user_id'] = $user_id;
			$where_day_buy_down['user_id'] = $user_id;
			$where_day_sale['user_id'] = $user_id;
			$where_day_sale_down['user_id'] = $user_id;
			$where_fee['user_id'] = $user_id;
			$where_fee_day['user_id'] = $user_id;
		}else{
			//获取所有会员
			$user_ids = Users::where(['act_id' => $this->agent_id])->column('id');
			$where_buy['user_id'] = ['in',$user_ids];;
			$where_sale['user_id'] = ['in',$user_ids];;
			$where_buy_down['user_id'] = ['in',$user_ids];;
			$where_sale_down['user_id'] = ['in',$user_ids];;
			$where_day_buy['user_id'] = ['in',$user_ids];;
			$where_day_buy_down['user_id'] = ['in',$user_ids];;
			$where_day_sale['user_id'] = ['in',$user_ids];;
			$where_day_sale_down['user_id'] = ['in',$user_ids];;
			$where_fee['user_id'] = ['in',$user_ids];;
			$where_fee_day['user_id'] = ['in',$user_ids];;
		}
		if(!empty($data['times'])) {
			$times = $data['times'];
			$times[0] = strtotime($times[0]);
			$times[1] = strtotime($times[1]);
			$where_buy['create_time'] = ['between',$times];
			$where_sale['create_time'] = ['between',$times];
			$where_buy_down['create_time'] = ['between',$times];
			$where_sale_down['create_time'] = ['between',$times];
			$where_fee['create_time'] = ['between',$times];
		}
		//总手续费
		$fee = Db::name('trade_leveraged')->where($where_fee)->sum('fee');
		//总盈利
		$loss_up_buy = Db::name('trade_leveraged')->where($where_buy)->sum('real_price');
		$loss_up_sale = Db::name('trade_leveraged')->where($where_sale)->sum('real_price');
		$loss_up = $loss_up_buy + $loss_up_sale;
		//总亏损
		$loss_down_buy = Db::name('trade_leveraged')->where($where_buy_down)->sum('real_price');
		$loss_down_sale = Db::name('trade_leveraged')->where($where_sale_down)->sum('real_price');
		$loss_down = $loss_down_buy + $loss_down_sale;
		//今日手续费
		$fee_day = Db::name('trade_leveraged')->where($where_fee_day)->sum('fee');
		//今日盈利
		$loss_up_buy = Db::name('trade_leveraged')->where($where_day_buy)->sum('real_price');
		$loss_up_sale = Db::name('trade_leveraged')->where($where_day_sale)->sum('real_price');
		$loss_up_day = $loss_up_buy + $loss_up_sale;
		//今日亏损
		$loss_down_buy = Db::name('trade_leveraged')->where($where_day_buy_down)->sum('real_price');
		$loss_down_sale = Db::name('trade_leveraged')->where($where_day_sale_down)->sum('real_price');
		$loss_down_day = $loss_down_buy + $loss_down_sale;
		$res = [
				'all' => [
						'fee' => $fee,
						'loss_up' => $loss_up,
						'loss_down' => $loss_down
				],
				'day' => [
						'fee' => $fee_day,
						'loss_up' => $loss_up_day,
						'loss_down' => $loss_down_day
				]
		];
		return $res;
	}
	
	/**
	 * 提币记录
	 * @return \think\response\View
	 */
	public function userCash()
	{
		$data = $this->request->param('');
		$mobile = !empty($data['mobile']) ? $data['mobile'] : '';
		$start_time = !empty($data['start_time']) ? $data['start_time'] : '';
		$end_time = !empty($data['end_time']) ? $data['end_time'] : '';
		$user_ids = Users::where(['act_id' => $this->agent_id])->column('id');
		$where = ['coin_id' => 5]; //初始化查询条件
		if($mobile) {
			$user_id = Users::where(['user_name' => $mobile])->value('id');
			$where['user_id'] =$user_id;
		}else{
			$where['user_id'] = ['in',$user_ids];
		}
		if($start_time && $end_time) {
			if(strtotime($end_time) > strtotime($start_time)) {
				$where['create_time'] = ['between',[strtotime($start_time),strtotime($end_time)]];
			}
		}
		$list = CashList::where($where)->order('id desc')->paginate(15,false,[
				'query' => $data
		]);
		$recharge_all = CashList::where(['user_id'=>$where['user_id'],'coin_id' => 5,'state' => 1])->sum('num');
		$start = strtotime(date('Y-m-d').'00:00:00');
		$end = strtotime(date('Y-m-d').'23:59:59');
		$rechage_day = CashList::where(['user_id'=>$where['user_id'],'coin_id' => 5,'state' => 1,'create_time' => ['between',[$start,$end]]])->sum('num');
		$page = $list->render();
		$this->assign('list',$list);
		$this->assign('page',$page);
		$this->assign('mobile',$mobile);
		$this->assign('start_time',$start_time);
		$this->assign('end_time',$end_time);
		$this->assign('recharge_all',$recharge_all);
		$this->assign('recharge_day',$rechage_day);
		return view();
	}
	
	/**
	 * 充币记录
	 * @return \think\response\View
	 */
	public function userRecharge()
	{
		$data = $this->request->param('');
		$mobile = !empty($data['mobile']) ? $data['mobile'] : '';
		$start_time = !empty($data['start_time']) ? $data['start_time'] : '';
		$end_time = !empty($data['end_time']) ? $data['end_time'] : '';
		$user_ids = Users::where(['act_id' => $this->agent_id])->column('id');
		$where = ['coin_id' => 5]; //初始化查询条件
		if($mobile) {
			$user_id = Users::where(['user_name' => $mobile])->value('id');
			$where['user_id'] =$user_id;
		}else{
			$where['user_id'] = ['in',$user_ids];
		}
		if($start_time && $end_time) {
			if(strtotime($end_time) > strtotime($start_time)) {
				$where['create_time'] = ['between',[strtotime($start_time),strtotime($end_time)]];
			}
		}
		$list = CashRecharge::where($where)->order('id desc')->paginate(15,false,[
				'query' => $data
		]);
		$recharge_all = CashRecharge::where(['user_id'=>$where['user_id'],'coin_id' => 5,'state' => 1])->sum('num');
		$start = strtotime(date('Y-m-d').'00:00:00');
		$end = strtotime(date('Y-m-d').'23:59:59');
		$rechage_day = CashRecharge::where(['user_id'=>$where['user_id'],'coin_id' => 5,'state' => 1,'create_time' => ['between',[$start,$end]]])->sum('num');
		$page = $list->render();
		$this->assign('list',$list);
		$this->assign('page',$page);
		$this->assign('mobile',$mobile);
		$this->assign('start_time',$start_time);
		$this->assign('end_time',$end_time);
		$this->assign('recharge_all',$recharge_all);
		$this->assign('recharge_day',$rechage_day);
		return view();
	}
}